package jp.sourceforge.akjrcp.audio;

public class MpegAudioFormatUtil {
public static final int MPEG_VERSION1=0;
public static final int MPEG_VERSION2=1;
public static final int MPEG_VERSION25=2;//unofficial
public static final int MPEG_LAYER1=1;
public static final int MPEG_LAYER2=2;
public static final int MPEG_LAYER3=0;

public static final int[] getAvailableFrameRate(int mpegVersion){
	if(mpegVersion==MPEG_VERSION1){
		return new int[]{44100,48000,32000};
	}else if(mpegVersion==MPEG_VERSION2){
		return new int[]{22050,24000,16000};
	}else if(mpegVersion==MPEG_VERSION25){
		return new int[]{11025,12000,8000};
	}
	return new int[0];
}
public static final int[] all_framerate={8000,11025,12000,16000,22050,24000,32000,44100,48000};
public static final int[] official_mp3_framerate={16000,22050,24000,32000,44100,48000};
public static final int[] common_mp3_bitrate={32,40,48,56,64,80,96,112,128,160};
                           
public static final int[] getAvailableBitrate(int MPEG_VERSION,int MPEG_LAYER){
	if(MPEG_VERSION==MPEG_VERSION1){
		if(MPEG_LAYER==MPEG_LAYER1){
			return new int[]{32,64,96,128,160,192,224,256,288,320,352,384,416,448};
		}else if(MPEG_LAYER==MPEG_LAYER2){
			return new int[]{32,48,56,64,80,96,112,128,160,192,224,256,320,384};
			
		}else if(MPEG_LAYER==MPEG_LAYER3){
			return new int[]{32,40,48,56,64,80,96,112,128,160,192,224,256,320};
		}
	}else if(MPEG_VERSION==MPEG_VERSION2 ||MPEG_VERSION==MPEG_VERSION25){
		if(MPEG_LAYER==MPEG_LAYER1){
			return new int[]{32,48,56,64,80,96,112,128,144,160,176,192,224,256};
			}else if(MPEG_LAYER==MPEG_LAYER2 || MPEG_LAYER==MPEG_LAYER3){
			return new int[]{8,16,24,32,40,48,56,64,80,96,112,128,144,160};
			
		}
	}
	
	//wrong param
	return new int[0];
}

public static final int getSimilarValue(int values[],int value){
	
	int s[]=new int[values.length];
	for (int i = 0; i < values.length; i++) {
		if(values[i]==value){
			return value;
		}
		s[i]=values[i]-value;
	}
	
	int m=values.length-1;//maximum just for compare.
	for (int i = 0; i < s.length-1; i++) {
		if(Math.abs(s[i])<Math.abs(s[m])){
			m=i;
		}else if(Math.abs(s[i])==Math.abs(s[m])){
			if(s[i]>s[m]){
				m=i; //set more bigger value.
			}
		}
	}
	
	
	return values[m];
}

public static final int[] getAvailableMP3Bitrate(int framerate){
	int version=getMpegVersion(framerate);
	return getAvailableBitrate(version,MPEG_LAYER3);
}

public static final int getMpegVersion(int framerate){
	int v1[]=getAvailableFrameRate(MPEG_VERSION1);
	for (int i = 0; i < v1.length; i++) {
		if(v1[i]==framerate){
			return MPEG_VERSION1;
		}
	}
	
	int v2[]=getAvailableFrameRate(MPEG_VERSION2);
	for (int i = 0; i < v2.length; i++) {
		if(v2[i]==framerate){
			return MPEG_VERSION2;
		}
	}
	
	int v25[]=getAvailableFrameRate(MPEG_VERSION25);
	for (int i = 0; i < v25.length; i++) {
		if(v25[i]==framerate){
			return MPEG_VERSION25;
		}
	}
	
	return -1;
}

}
